  
  
/*  
* Copyright @ 2017 com.iflysse.trains  
* xebest-erp 上午11:06:52  
* All right reserved.  
*  
*/  
  
package com.xebest.web.service.impl;  

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xebest.common.enums.erp.storehouse.VenStoreStatusEnum;
import com.xebest.common.enums.erp.sys.VenDelEnum;
import com.xebest.common.util.XeBaseUtil;
import com.xebest.util.seq.StoreHouseSeqUtil;
import com.xebest.web.form.XeVenStoreInfoUpdateForm;
import com.xebest.web.mapper.read.XeVenStoreCoverAreaReadMapper;
import com.xebest.web.mapper.read.XeVenStoreInfoReadMapper;
import com.xebest.web.mapper.write.XeVenStoreCoverAreaWriteMapper;
import com.xebest.web.mapper.write.XeVenStoreInfoWriteMapper;
import com.xebest.web.model.XeVenStoreCoverAreaEntity;
import com.xebest.web.model.XeVenStoreInfoEntity;
import com.xebest.web.service.XeVenStoreInfoService;
import com.xebest.web.view.ErpUserInfo;
import com.xebest.web.view.VenStoreRepeatCoverAreaView;
import com.xebest.web.view.XeVenStoreCoverAreaView;
import com.xebest.web.view.XeVenStoreInfoView;

  
/**  
 * @desc: xebest-erp  
 * @author: 杜春静 
 * @createTime: 2017年6月14日 上午11:06:52  
 * @history:  
 * @version: v1.0    
 */

@Service
public class XeVenStoreInfoServiceImpl implements XeVenStoreInfoService {
	
	@Autowired
	private XeVenStoreInfoReadMapper xeVenStoreInfoReadMapper;
	
	@Autowired
	private XeVenStoreCoverAreaReadMapper  xeVenStoreCoverAreaReadMapper;
	
	@Autowired
	private XeVenStoreInfoWriteMapper xeVenStoreInfoWriteMapper;
	
	@Autowired
	private XeVenStoreCoverAreaWriteMapper  xeVenStoreCoverAreaWriteMapper;
	
	private static final Logger log = LoggerFactory.getLogger(XeVenStoreRoleServiceImpl.class);//日志记录类
	 
	
	  
	/**
	 * 查询仓库列表
	 */
	@Override
	public List<XeVenStoreInfoView> listByStoreInfoEntity(
			Map<String, Object> paramMap) {
		List<XeVenStoreInfoView>  storeViewList = xeVenStoreInfoReadMapper.listByParam(paramMap);
		for(XeVenStoreInfoView view : storeViewList){
			String area = XeBaseUtil.baseUtil.getOrgAreaName(view.getAreaId());
			view.setArea(area);
		}
		
		return storeViewList; 
		
	}

	  
	/**
	 * 添加仓库信息
	 * @author: 杜春静  
	 * @createTime: 2017年6月15日 下午4:52:15  
	 * @history:  
	 * @param entity
	 * @return int
	 */
	@Override
	@Transactional(value="writeTransactionManager")
	public Map<String, Object> insertStoreInfo(XeVenStoreInfoUpdateForm form,ErpUserInfo userInfo) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		//  生成仓库的业务编码
		String storeId = form.getStoreId();
		if(StringUtils.isEmpty(storeId)){
			storeId = StoreHouseSeqUtil.seqUtils.createStoreIdNo(userInfo.getVenSimpleCode().replace("_", ""));
		}
		int result = 0;
		//转换数据，并设置仓库的的一些商户信息
		XeVenStoreInfoEntity entity = new XeVenStoreInfoEntity();
		entity.setStoreId(storeId);
		entity.setStoreName(form.getStoreName());
		entity.setStoreStatus(form.getStoreStatus());
		entity.setStoreTel(form.getStoreTel());
		entity.setContactPerson(form.getContactPerson());
		entity.setProvinceId(form.getProvinceId());
		entity.setCityId(form.getCityId());
		entity.setAreaId(form.getAreaId());
		entity.setStoreAddress(form.getStoreAddress());
		entity.setStoreWarm(form.getStoreWarm());
		entity.setStoreType(form.getStoreType());
		entity.setStoreUse(form.getStoreUse());
		entity.setStoreAcreage(form.getStoreAcreage());
		entity.setStoreUseAcreage(form.getStoreUseAcreage());
		entity.setWmsStatus(form.getWmsStatus());
		entity.setTmsStatus(form.getTmsStatus());
		entity.setNegativeFlag(form.getNegativeFlag());
		entity.setRecordInventoryFlag(form.getRecordInventoryFlag());
		entity.setUserCode(userInfo.getUserCode());
		entity.setCreateUser(userInfo.getLoginName());
		entity.setModifyUser(entity.getModifyUser());
	   
		
		//查询同一商户下仓库名称是否存在
		Long storeCount = xeVenStoreInfoWriteMapper.countBySotreInfo(entity);
		if(storeCount > 0){
			resultMap.put("result", "0002");
			return resultMap;
		}
		
		List<XeVenStoreCoverAreaEntity> coverAreaList = new ArrayList<XeVenStoreCoverAreaEntity>();
		//封装覆盖区域
		String[] coverAreaArray = form.getCoverAreaArray();
		
		if(null != coverAreaArray  && coverAreaArray.length >0){
			
			//如果仓库状态为启用状态，判断仓库的覆盖区域是否冲突
			if(VenStoreStatusEnum.YES.getValue().equals(entity.getStoreStatus())){
				
				String repeatCoverStr = this.queryRepeatStoreArea(storeId,coverAreaArray,userInfo.getUserCode());
				if(!StringUtils.isEmpty(repeatCoverStr)){
					resultMap.put("result", "0001");
					resultMap.put("coverArea", repeatCoverStr);
					return resultMap;
				}
			}
			
			for(String coverArea : coverAreaArray){
				if(StringUtils.isEmpty(coverArea)){
					resultMap.put("result", "0002");
					return resultMap;
				}
				XeVenStoreCoverAreaEntity coverAreaEntity = new XeVenStoreCoverAreaEntity();
				//设置区域编码
				coverAreaEntity.setCoverAreaId(Integer.valueOf(coverArea));
				//查询区域orgcode
				Integer areaLevel = XeBaseUtil.baseUtil.getAreaLevel(Integer.valueOf(coverArea));
				coverAreaEntity.setAreaLevel(areaLevel);

	            //区域orgcode
	            String areaOrgCode = XeBaseUtil.baseUtil.getAreaOrgCode(Integer.valueOf(coverArea));
	            coverAreaEntity.setAreaOrgCode(areaOrgCode);
	            coverAreaList.add(coverAreaEntity);
			}
		}
		
		
		//插入仓库主表信息
		result = xeVenStoreInfoWriteMapper.insertStoreInfo(entity);
		if(result > 0){//如果仓库主表插入成功
			
			if(null != coverAreaList&& coverAreaList.size() > 0){ 
				
					//修改仓库信息--先删除仓库的覆盖区域
					result =xeVenStoreCoverAreaWriteMapper.deleteByStoreId(storeId);
					//添加仓库的覆盖区域
					result = xeVenStoreCoverAreaWriteMapper.insertCoverArea(coverAreaList,storeId);
			}
		}
		resultMap.put("result",String.valueOf(result));
		return resultMap;  
	}
	
	/**
	 * 修改仓库信息
	 * @author: 杜春静  
	 * @createTime: 2017年6月15日 下午4:54:03  
	 * @history:  
	 * @param entity
	 * @return stirng
	 */
	@Override
	@Transactional(value="writeTransactionManager")
	public Map<String, Object> updateStoreInfo(XeVenStoreInfoUpdateForm form,ErpUserInfo userInfo) throws Exception{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		int updateResult = 0;
		String storeId = form.getStoreId();
		//转换仓库数据
		XeVenStoreInfoEntity entity = new XeVenStoreInfoEntity();
		entity.setStoreId(storeId);
		entity.setStoreName(form.getStoreName());
		entity.setStoreStatus(form.getStoreStatus());
		entity.setStoreTel(form.getStoreTel());
		entity.setContactPerson(form.getContactPerson());
		entity.setProvinceId(form.getProvinceId());
		entity.setCityId(form.getCityId());
		entity.setAreaId(form.getAreaId());
		entity.setStoreAddress(form.getStoreAddress());
		entity.setStoreWarm(form.getStoreWarm());
		entity.setStoreType(form.getStoreType());
		entity.setStoreUse(form.getStoreUse());
		entity.setStoreAcreage(form.getStoreAcreage());
		entity.setStoreUseAcreage(form.getStoreUseAcreage());
		entity.setWmsStatus(form.getWmsStatus());
		entity.setTmsStatus(form.getTmsStatus());
		entity.setNegativeFlag(form.getNegativeFlag());
		entity.setRecordInventoryFlag(form.getRecordInventoryFlag());
		//设置商户信息
		entity.setUserCode(userInfo.getUserCode());
		entity.setModifyUser(userInfo.getLoginName());
		
		//查询同一商户下仓库名称是否存在
		Long storeCount = xeVenStoreInfoWriteMapper.countBySotreInfo(entity);
		if(storeCount > 0){
			resultMap.put("result", "0002");
			return resultMap;
		}
		
		List<XeVenStoreCoverAreaEntity> coverAreaList = new ArrayList<XeVenStoreCoverAreaEntity>();
		//封装覆盖区域
		String[] coverAreaArray = form.getCoverAreaArray();
		if(null != coverAreaArray && coverAreaArray.length >0){
			
			//如果仓库状态为启用状态，判断仓库的覆盖区域是否冲突
			if(VenStoreStatusEnum.YES.getValue().equals(entity.getStoreStatus())){
				
				String repeatCoverStr = this.queryRepeatStoreArea(storeId,coverAreaArray,userInfo.getUserCode());
				if(!StringUtils.isEmpty(repeatCoverStr)){
					resultMap.put("result", "0001");
					resultMap.put("coverArea", repeatCoverStr);
					return resultMap;
				}
			}
			
			for(String coverArea : coverAreaArray){
				
				if(StringUtils.isEmpty(coverArea)){
					if(StringUtils.isEmpty(coverArea)){
						resultMap.put("result", "0002");
						return resultMap;
					}
				}
				XeVenStoreCoverAreaEntity coverAreaEntity = new XeVenStoreCoverAreaEntity();
				//设置区域编码
				coverAreaEntity.setCoverAreaId(Integer.valueOf(coverArea));
				//查询区域orgcode
				Integer areaLevel = XeBaseUtil.baseUtil.getAreaLevel(Integer.valueOf(coverArea));
				coverAreaEntity.setAreaLevel(areaLevel);

	            //区域orgcode
	            String areaOrgCode = XeBaseUtil.baseUtil.getAreaOrgCode(Integer.valueOf(coverArea));
	            coverAreaEntity.setAreaOrgCode(areaOrgCode);
	            coverAreaList.add(coverAreaEntity);
			}
		} else {
			//修改仓库信息--先删除仓库的覆盖区域
			updateResult =xeVenStoreCoverAreaWriteMapper.deleteByStoreId(storeId);
		}
		
		//更新仓库基本信息
		updateResult =xeVenStoreInfoWriteMapper.updateStoreInfoById(entity);
		//TODO 覆盖区域修改
		if(updateResult > 0){
			if(null != coverAreaList&& coverAreaList.size() > 0){ 
				
					//修改仓库信息--先删除仓库的覆盖区域
					updateResult =xeVenStoreCoverAreaWriteMapper.deleteByStoreId(storeId);

					// 添加仓库的覆盖区域
					updateResult = xeVenStoreCoverAreaWriteMapper.insertCoverArea(coverAreaList,storeId);
			}
		}
		resultMap.put("result", String.valueOf(updateResult));
		return resultMap;  
		
	}
	/**
	 * 启用或禁用仓库
	 */
	@Transactional(value="writeTransactionManager")
	@Override
	public Map<String, Object> enableOrDisableStore(String storeId,int storeStatus,String userCode) throws Exception{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		XeVenStoreInfoEntity entity = new XeVenStoreInfoEntity();
		entity.setStoreId(storeId);//设置要更新的仓库
		entity.setUserCode(userCode);//设置要更新的用户code
		XeVenStoreCoverAreaEntity coverArea = new XeVenStoreCoverAreaEntity();
		coverArea.setStoreId(storeId);
		int updateResult = 0;
		//禁用仓库
		if(storeStatus == VenStoreStatusEnum.NO.getValue()){
			entity.setStoreStatus(VenStoreStatusEnum.NO.getValue());
			//禁用仓库--更新仓库信息
			updateResult =xeVenStoreInfoWriteMapper.updateStoreInfoById(entity);
			coverArea.setDelFlag(VenDelEnum.YES.getValue());//仓库的覆盖区域置为已删除
			if(updateResult > 0){
				//禁用仓库--更新仓库覆盖区域信息
				 xeVenStoreCoverAreaWriteMapper.updateStoreCoverAreaByStoreId(coverArea);
			}
			
		} else if(storeStatus == VenStoreStatusEnum.YES.getValue()){//启用
			
			entity.setStoreStatus(VenStoreStatusEnum.YES.getValue());
			coverArea.setDelFlag(VenDelEnum.NO.getValue());//仓库的覆盖区域置为未删除
			//启用仓库--更新仓库覆盖区域信息
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("storeId", storeId);
			params.put("userCode", userCode);
			//查询仓库的重复的覆盖区域
			// 覆盖区域重新修改
			String[] coverAreaArray = this.xeVenStoreCoverAreaReadMapper.judgeEnableStore(params);
			
			if(null != coverAreaArray && coverAreaArray.length > 0){
				String coverAreaStr = this.queryRepeatStoreArea(storeId,coverAreaArray,userCode);
				if(!StringUtils.isEmpty(coverAreaStr)){
					resultMap.put("result", "0001");
					resultMap.put("coverArea", coverAreaStr);
					return resultMap;
				}
			}
			
			//更新仓库主表
			updateResult =xeVenStoreInfoWriteMapper.updateStoreInfoById(entity);
			if(updateResult > 0){
				//更新仓库的覆盖区域
				xeVenStoreCoverAreaWriteMapper.updateStoreCoverAreaByStoreId(coverArea);
			}
		}
		resultMap.put("result", String.valueOf(updateResult));
		return resultMap;
	}


	/**
	 * 暂时不用
	 * @author: 杜春静
	 * @createTime: 2017年6月15日 下午4:57:54
	 * @history:
	 * @param storeId,cityIds,userCode
	 * @return List<VenStoreRepeatCoverAreaView>
	 */
	@Override
	public List<VenStoreRepeatCoverAreaView> listOtherStoreCoverArea(
			String storeId,String[] coverAreaArray,String userCode) {
		Map<String, Object>  params = new HashMap<String, Object>();
		
		params.put("storeId", storeId);
		params.put("userCode", userCode);
		
		//params.put("coverAreaArray", coverArrayToList(coverAreaArray));
		List<VenStoreRepeatCoverAreaView> listCoverArea = xeVenStoreCoverAreaReadMapper.listOtherStoreCoverArea(params);
		//设置区域的区域名称
		for(VenStoreRepeatCoverAreaView coverArea : listCoverArea){
			coverArea.setCoverAreaName(XeBaseUtil.baseUtil.getOrgAreaName(coverArea.getCoverAreaId()));
		}
		
		return listCoverArea;  
	}
	
	
	  
	/**
	 * 根据仓库信息统计已有的仓库信息 例如仓库名称是否存在
	 * @author: 杜春静  
	 * @createTime: 2017年6月15日 下午5:02:54  
	 * @history:  
	 * @param entity
	 * @return int
	 */
	@Override
	public long countByStoreInfo(XeVenStoreInfoEntity entity) {
		  
		return xeVenStoreInfoWriteMapper.countBySotreInfo(entity);  
		
	}

	/**
	 * 根据仓库id查询仓库类型
	 * @author: Wangyp
	 * @createTime: 2017/9/4 17:46
	 * @param :
	 * @return:
	 */
	public XeVenStoreInfoEntity getStoreTypeByStoreId(String storeId, String userCode){
		return  xeVenStoreInfoReadMapper.getStoreTypeByStoreId(storeId,userCode);
	}



	/**
	 * 获取仓库列表
	 */
	@Override
	public List<Map<String, Object>> listStoreName(Map<String, Object> param) {
		
		return xeVenStoreInfoWriteMapper.listStoreNameByParam(param);  
		
	}
	  
	
	/**
	 * 根据仓库编码查询仓库的基本信息
	 */
	@Override
	public XeVenStoreInfoEntity getStoreInfoByMap(String storeId,String userCode) {

			XeVenStoreInfoEntity entity = xeVenStoreInfoReadMapper.getStoreInfoByMap(storeId, userCode);
			JSONArray coverAreaArray = new JSONArray();

			if(entity != null){
				//查询当前仓库下的覆盖区域
				List<XeVenStoreCoverAreaView> coverAreaList = xeVenStoreCoverAreaReadMapper.listCoverAreaViewByStoreId(entity.getStoreId());
				if(null != coverAreaList && coverAreaList.size() > 0){
					coverAreaArray =queryCoverAreaList(coverAreaList, null);
					//省信息
					Map<String, XeVenStoreCoverAreaView> pMap = new HashMap<>();
					//市信息
					Map<String, XeVenStoreCoverAreaView> cMap = new HashMap<>();
					//区信息
					Map<String, XeVenStoreCoverAreaView> aMap = new HashMap<>();
					Integer areaId;
					Integer pId = 0;
					Integer cId = 0;
					String coverAreaName;
					for(XeVenStoreCoverAreaView coverAreaEntity : coverAreaList){
						coverAreaName = XeBaseUtil.baseUtil.getAreaName(coverAreaEntity.getAreaId());
						coverAreaEntity.setAreaName(coverAreaName);

						areaId = coverAreaEntity.getAreaId();

						Integer areaLevel = coverAreaEntity.getAreaLevel();
						if (areaLevel == 1) {
							pMap.put(areaId.toString(), coverAreaEntity);
						} else if (areaLevel == 2) {
							//2.二级城市,设置父级省城市信息
							cMap.put(areaId.toString(), coverAreaEntity);
							//获得当前市的省id
							pId = XeBaseUtil.baseUtil.getAreaParentId(areaId);
							//设置省信息
							setAreaInfoByAreaId(pMap, pId);
						} else if (areaLevel == 3) {
							//2.三级城市,设置父级省市的信息
							aMap.put(areaId.toString(), coverAreaEntity);
							//获得市id
							cId = XeBaseUtil.baseUtil.getAreaParentId(areaId);
							if (cMap.get(cId.toString()) == null) {
								//获得省的市名称
								setAreaInfoByAreaId(cMap, cId);
								//获得市的省id
								pId = XeBaseUtil.baseUtil.getAreaParentId(cId);
								setAreaInfoByAreaId(pMap, pId);
							}
						}
					}
					JSONObject areaArray;
					JSONObject cityArray;
					String areaIdStr;
					JSONObject pSaleArea;
					JSONObject cityArea;
					String cityId;
					XeVenStoreCoverAreaView cArea;
					JSONArray citySaleAreaList;
					JSONArray areaAreaList;
					String aAreaId;
					XeVenStoreCoverAreaView pArea;
					XeVenStoreCoverAreaView aArea;
					for(int i = 0,count = coverAreaArray.size(); i < count; i++){
						areaArray = coverAreaArray.getJSONObject(i);
						areaIdStr = areaArray.getString("areaId");
						pArea = pMap.get(areaIdStr);
						if (pArea != null){
							if (pArea.getAreaId() != null && pArea.getAreaId() != null && pArea.getAreaId() != 0) {
								//省级销售区域id不为空，说明该数据属于省级销售数据
								pSaleArea = JSONObject.parseObject(JSONObject.toJSONString(pArea));

								coverAreaArray.set(i, pSaleArea);
								citySaleAreaList = areaArray.getJSONArray("areaList");
								if(citySaleAreaList != null && citySaleAreaList.size() > 0){
									for (int j = 0; j < citySaleAreaList.size(); j++){
										cityArray = citySaleAreaList.getJSONObject(j);
										cityId = cityArray.getString("areaId");
										cArea = cMap.get(cityId);
										if (cArea != null) {
											if (cArea.getAreaId() != null && cArea.getAreaId() != null && cArea.getAreaId() != 0) {
												cityArea = JSONObject.parseObject(JSONObject.toJSONString(cArea));
												//当前市属于市级销售商品
												citySaleAreaList.set(j, cityArea);
												//当前市下有区级销售商品
												//获得当前市下所有区级城市信息
												areaAreaList = cityArray.getJSONArray("areaList");
												if (areaAreaList != null && areaAreaList.size() > 0) {
													for (int k = 0; k < areaAreaList.size(); k++) {
														aAreaId = areaAreaList.getJSONObject(k).getString("areaId");
														aArea = aMap.get(aAreaId);
														if (aArea != null) {
															JSONObject aAreaJO = JSONObject.parseObject(JSONObject.toJSONString(aArea));
															//区域是否勾选1勾选、2不勾选
															aAreaJO.put("areaCheckBoxFlag", 1);
															areaAreaList.set(k, aAreaJO);
														}
													}
													cityArea.put("areaCheckBoxFlag", 2);
												} else {
													//区域是否勾选1勾选、2不勾选
													cityArea.put("areaCheckBoxFlag", 1);
												}
												//给当前市设置数据
												cityArea.put("areaList", areaAreaList);
											}
										}
									}
									pSaleArea.put("areaCheckBoxFlag", 2);
								} else {
									//区域是否勾选1勾选、2不勾getCoverAreaId选
									pSaleArea.put("areaCheckBoxFlag", 1);
								}
								pSaleArea.put("areaList", citySaleAreaList);

							}
						}
					}

				}
			}
			entity.setCoverAreaList(coverAreaArray);
		return entity;  
		
	}
	
	
	public JSONArray  queryCoverAreaList(List<XeVenStoreCoverAreaView> coverAreaList, Integer areaId){
		
		JSONArray coverAreaArray = new JSONArray();
		if( areaId == null){
			if(!CollectionUtils.isEmpty(coverAreaList)){
					Integer aId;
					Integer areaLevel;
					XeVenStoreCoverAreaView coverAreaEntity;
					//当前仓库覆盖区域是部分区域，查询指定省信息
					for (int i = 0, len = coverAreaList.size(); i < len; i ++) {
						//根据id获得城市信息
						coverAreaEntity = coverAreaList.get(i);
						aId = coverAreaEntity.getAreaId();
	                    areaLevel = coverAreaEntity.getAreaLevel();
	                	JSONObject areaJson = new JSONObject();
                    	areaJson.put("areaId", aId);
                    	areaJson.put("areaName", XeBaseUtil.baseUtil.getAreaName(aId));
                    	coverAreaArray =coverArrayAddObject(coverAreaArray,areaJson,areaLevel);
	                	
					}
			}
		} else {
            //查询指定区域id下区域信息
			coverAreaArray = XeBaseUtil.baseUtil.getChildArea(areaId);
        }
		
		if (!CollectionUtils.isEmpty(coverAreaArray)) {
            for (int i = 0; i < coverAreaArray.size(); i++) {
                JSONObject js = coverAreaArray.getJSONObject(i);
                //区域是否勾选1勾选、2不勾选
                js.put("areaCheckBoxFlag", 2);
                JSONArray areaList = js.getJSONArray("areaList");
                if(null != areaList && areaList.size() > 0){
					 for(int k = 0; k < areaList.size(); k ++){
						 JSONObject cJsonObject = (JSONObject)areaList.get(k);
						 cJsonObject.put("areaCheckBoxFlag", 2);
						 JSONArray aListOld = cJsonObject.getJSONArray("areaList");
						 if(null != aListOld){
							 for(int h = 0; h < aListOld.size(); h ++){
								 JSONObject aJsonObject = (JSONObject)aListOld.get(h);
								 aJsonObject.put("areaCheckBoxFlag", 2);
							 }
						 }
						 
					 }
				 }
                
            }
        }
		return coverAreaArray;
	}
	
	public JSONArray coverArrayAddObject(JSONArray coverAreaArray, JSONObject pJson, Integer areaLevel){
		if(areaLevel == 1){
			for(int j = 0;j < coverAreaArray.size(); j++){
				JSONObject newJsonObjectI = (JSONObject)coverAreaArray.get(j);
				 String  areaIdI = newJsonObjectI.get("areaId").toString();
				 String areaIdJ = pJson.get("areaId").toString();
				 if(areaIdI.equals(areaIdJ)){
					 return coverAreaArray;
				} 
			}
			coverAreaArray.add(pJson);
			 return coverAreaArray;
			
		}
		if(areaLevel == 2){
			 String areaIdJ = pJson.get("areaId").toString();
			 Integer pId = XeBaseUtil.baseUtil.getAreaParentId(Integer.valueOf(areaIdJ));
			//如果包含
			 for(int j = 0;j < coverAreaArray.size(); j++){
				 JSONObject newJsonObjectI = (JSONObject)coverAreaArray.get(j);
				 String  areaIdI = newJsonObjectI.get("areaId").toString();
				 if(areaIdI.equals(pId.toString())){
					 JSONArray areaList = newJsonObjectI.getJSONArray("areaList");
					 if( null == areaList){
						 newJsonObjectI.put("areaList", XeBaseUtil.baseUtil.getChildArea(pId));
						 return coverAreaArray;
					 } else {
						 return coverAreaArray;
					 }
				 } 
			}
			//如果不包含
			JSONObject pJson2 = new JSONObject();
         	pJson2.put("areaId", pId);
         	pJson2.put("areaName", XeBaseUtil.baseUtil.getAreaName(pId));
         	pJson2.put("areaList", XeBaseUtil.baseUtil.getChildArea(pId));
         	coverAreaArray.add(pJson2);
         	 return coverAreaArray;
		}
		
		if(areaLevel == 3){
			 String areaIdJ = pJson.get("areaId").toString();
			 //获得区ID
			 Integer aId = Integer.valueOf(areaIdJ);
			 
			 //获得市id
			 Integer cId = XeBaseUtil.baseUtil.getAreaParentId(aId);
			//获得市的省id
			 Integer pId = XeBaseUtil.baseUtil.getAreaParentId(cId);
			 
			 for(int j = 0;j < coverAreaArray.size(); j++){
				 JSONObject newJsonObjectI = (JSONObject)coverAreaArray.get(j);
				 String  areaIdI = newJsonObjectI.get("areaId").toString();
				 //查询是否存在省
				 if(areaIdI.equals(pId.toString())){
					 JSONArray areaList = newJsonObjectI.getJSONArray("areaList");
					 if( null == areaList){
						 JSONArray cList = XeBaseUtil.baseUtil.getChildArea(pId);
						 newJsonObjectI.put("areaList", cList);
						 for(int k = 0; k < cList.size(); k ++){
							 JSONObject cJsonObject = (JSONObject)cList.get(k);
							 String cIdK = cJsonObject.get("areaId").toString();
							 if(cIdK.equals(cId.toString())){
								 JSONArray aList = XeBaseUtil.baseUtil.getChildArea(cId);
								 cJsonObject.put("areaList", aList);
							 }
						 }
						 return coverAreaArray;
					 } else if(null != areaList && areaList.size() > 0){
						 for(int k = 0; k < areaList.size(); k ++){
							 JSONObject cJsonObject = (JSONObject)areaList.get(k);
							 String cIdK = cJsonObject.get("areaId").toString();
							 if(cIdK.equals(cId.toString())){
								 JSONArray aListOld = cJsonObject.getJSONArray("areaList");
								 if(null == aListOld){
									 JSONArray aList = XeBaseUtil.baseUtil.getChildArea(cId);
									 cJsonObject.put("areaList", aList);
								 }
								 
							 }
						 }
						 return coverAreaArray;
					 }
				 }
				 
			 }
			 
			//如果不包含
			JSONObject pJson2 = new JSONObject();
         	pJson2.put("areaId", pId);
         	pJson2.put("areaName", XeBaseUtil.baseUtil.getAreaName(pId));
         	JSONArray cList = XeBaseUtil.baseUtil.getChildArea(pId);
         	pJson2.put("areaList", cList);
			 for(int k = 0; k < cList.size(); k ++){
				 JSONObject cJsonObject = (JSONObject)cList.get(k);
				 String cIdK = cJsonObject.get("areaId").toString();
				 if(cIdK.equals(cId.toString())){
					 JSONArray aList = XeBaseUtil.baseUtil.getChildArea(cId);
					 cJsonObject.put("areaList", aList);
				 }
			 }
         	coverAreaArray.add(pJson2);
         	 return coverAreaArray;
		}
		return coverAreaArray;
	}
	/**
	 * 查询区域ID是否在区域Map里，如果没有，则添加
	 * @author: 杜春静  
	 * @createTime: 2017年9月8日 上午11:40:58  
	 * @history:  
	 * @param areaMap
	 * @param areaId void
	 */
	 public void setAreaInfoByAreaId(Map<String, XeVenStoreCoverAreaView> areaMap, Integer areaId) {
	        //设置省信息
	        if (areaMap.get(areaId.toString()) == null) {
	            //获得并设置省名称
	            String areaName = XeBaseUtil.baseUtil.getAreaName(areaId);
	            XeVenStoreCoverAreaView area = new XeVenStoreCoverAreaView();
	            area.setAreaId(areaId);
	            area.setAreaName(areaName);
	            areaMap.put(areaId.toString(), area);
	        }
	    }
	
	/**
	 * 区域数组转化为区域List
	 * @author: 杜春静  
	 * @createTime: 2017年7月31日 下午7:08:42  
	 * @history:  
	 * @param coverAreaArray
	 * @return List<String>
	 */
 /* public List<String> coverArrayToList(String[] coverAreaArray){
	  List<String> areaCodeList = new ArrayList<String>();
		for(int i = 0; i < coverAreaArray.length; i++){
			String areaOrgCode = XeBaseUtil.baseUtil.getAreaOrgCode(Integer.valueOf(coverAreaArray[i]));
			String[] orgCodeStr = areaOrgCode.split("_");
			areaCodeList =  Arrays.asList(orgCodeStr); 
		}
		return areaCodeList;
  }*/
  
  public String queryRepeatStoreArea(String storeId,String[] coverAreaArray,String userCode) {
	  StringBuffer resultMessage = new StringBuffer();
	    Map<String, Object>  params = new HashMap<String, Object>();
		params.put("storeId", storeId);
		params.put("userCode", userCode);
		for(int i = 0; i < coverAreaArray.length; i++){
			Integer areaId = Integer.valueOf(coverAreaArray[i]);
			String areaOrgCode = XeBaseUtil.baseUtil.getAreaOrgCode(areaId);
			String areaOrgName = XeBaseUtil.baseUtil.getOrgAreaName(areaId);
			Integer areaLevel = XeBaseUtil.baseUtil.getAreaLevel(areaId);
			String[] orgCodeStr = areaOrgCode.split("_");
			if(areaLevel != null && areaLevel != 0 ){
				params.put("areaLevel", areaLevel);
				if(areaLevel == 1){
					params.put("coverAreaPro", orgCodeStr[1]);
				}
				if(areaLevel == 2){
					params.put("coverAreaPro", orgCodeStr[1]);
					params.put("coverAreaCity", orgCodeStr[2]);
				}
				if(areaLevel == 3){
					params.put("coverAreaPro", orgCodeStr[1]);
					params.put("coverAreaCity", orgCodeStr[2]);
					params.put("coverArea", orgCodeStr[3]);
				}
			}
			
			//查询是否有冲突的覆盖区域
			List<VenStoreRepeatCoverAreaView> listCoverArea = xeVenStoreCoverAreaReadMapper.listOtherStoreCoverArea(params);
			
			if(null != listCoverArea && listCoverArea.size() > 0){
				resultMessage.append("当前仓库覆盖区域："+areaOrgName+"与仓库：" );
				for(VenStoreRepeatCoverAreaView coverAreaView : listCoverArea){
					resultMessage.append(coverAreaView.getStoreName()); 
				}
				resultMessage.append("重复;");
			}
			
		}
		
		return resultMessage.toString();  
	}
  
}
  
